﻿using MediatR;
using S.Parturition.Domain.Model;
using S.Parturition.Infrastructure.Interface;
using S.Parturition.Read.API.Application.Command;

namespace S.Parturition.Read.API.Application.CommandHandlers
{
    /// <summary>
    /// 后裔根据种母耳号获取到最新的信息具体实现
    /// </summary>
    public class GetParturitionByLambHandler : IRequestHandler<GetParturitionByLambCommand, ParturitionModel>
    {

        private readonly IParturitionRepository repository;

        public GetParturitionByLambHandler(IParturitionRepository repository)
        {
            this.repository = repository;
        }

        public async Task<ParturitionModel> Handle(GetParturitionByLambCommand request, CancellationToken cancellationToken)
        {

            return repository.GetAllAsync().Result.Where(x => x.EarTag == request.EarTag).OrderByDescending(x => x.CreateDate).FirstOrDefault();

        }
    }
}
